from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from .models import Users
import base64,hashlib,json


class LoginAuth(BaseAuthentication):

    def authenticate(self, request):
        taken = request.META.get('HTTP_TOKEN')
        a = taken.split('.')
        if a[2]==hashlib.md5((a[0]+a[1]).encode()).hexdigest():
            decoded_data = json.loads(base64.b64decode(a[1]))
            user = decoded_data.get('username')
            return (user, taken)
        else:
            raise AuthenticationFailed('很抱歉，您没有登陆，不能操作')
